package cn.com.dhcc.ddi.datasource.service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.dhcc.app.core.exception.DaoException;
import cn.com.dhcc.app.core.exception.NiceException;
import cn.com.dhcc.app.core.exception.ServiceException;
import cn.com.dhcc.app.core.handler.dbhandler.SingleTransationCircleWithOutResult;
import cn.com.dhcc.app.core.service.BaseService;
import cn.com.dhcc.ddi.datasource.dao.DataSourceSQLserverExtDao;
import cn.com.dhcc.ddi.datasource.vo.DataSourceVo;
import cn.com.dhcc.ddi.datasource.vo.TableColVo;
import cn.com.dhcc.ddi.datasource.vo.TableInfoVo;
/**
 * 
 * SQL Server 2000 服务类
 * @Author: fxx
 * @date: 2017-5-22
 *
 */
@Service
public class DataSourceSQLserverExtService extends BaseService {
    @Autowired
    private DataSourceSQLserverExtDao dao;
    /**
     * 
     * @MethodName: [getUserTable]
     * @Description:[获取SQL Server 2000 用户表信息]
     * @Author:     [fxx]
     * @CreateDate: [2017-5-16]
     * @Params:     [ParamType ParamValue,ParamType ParamValue...]
     * @See         [Related classes and methods]
     * @Since       [Product template version]  
     * @Return:     [List<TableInfoVo>]
     * @Throws:     [Exception]
     *
     */
    public List<TableInfoVo> getUserTable(DataSourceVo vo) throws NiceException, ServiceException {
        try {
            //根据数据源初始化dao
            dao.initDao(vo);
            final List<TableInfoVo> list = new ArrayList<TableInfoVo>();
            final List<TableInfoVo> userTableList  = getTables(vo);
            dao.getDao().doInSingleTransationCircle("元数据抽取", new SingleTransationCircleWithOutResult() {
                @Override
                public void actionInCircle() throws RuntimeException {
                    try {
                        for (Iterator<TableInfoVo> iterator = userTableList.iterator(); iterator.hasNext();) {
                            TableInfoVo userTable = iterator.next();
                            List<TableColVo> cols = dao.getUserTabColumnsForSqlserverext(userTable.getSchema(), userTable.getTableName());
                            userTable.setTableColVos(cols);
                            list.add(userTable);
                        }
                    } catch (DaoException e) {
                        throw new RuntimeException("获取表元数据错误", e);
                    }
                }
            });
            return list;
        } catch (DaoException e) {
            throw new ServiceException("获取表元数据错误", e);
        }
    }
    /**
     * 抽取表结构，包括授权和自建的表和视图  sqlserver
     * @return
     * @throws ServiceException
     */
    public List<TableInfoVo> getTables(DataSourceVo vo) throws ServiceException {
        List<TableInfoVo> userTableList = new ArrayList<TableInfoVo>();
        try {
            //抽取授权的表和视图
            userTableList.addAll( dao.getUserTablesForSqlserverext());
        } catch (DaoException e) {
            throw new ServiceException("获取表元数据错误", e);
        }
        return userTableList;
    }

    /**
     * 
     * @MethodName: [getUserTablesName]
     * @Description:[获取用户所有表名]
     * @Author:     [fxx]
     * @CreateDate: [2017-5-16]
     * @Params:     [ParamType ParamValue,ParamType ParamValue...]
     * @See         [Related classes and methods]
     * @Since       [Product template version]  
     * @Return:     [List<String>]
     * @Throws:     [Exception]
     *
     */
    public List<String> getUserTablesName(DataSourceVo vo, String dsUserName) throws ServiceException, NiceException {
        try {
            dao.initDao(vo);
            List<String> userTableList =  dao.getUserTablesNameForSqlserverext();
            return userTableList;
        } catch (DaoException e) {
            throw new ServiceException("获取表元数据错误", e);
        }
    }
    /**
     * 
     * @MethodName: [getUserTablesName]
     * @Description:[获取用户所有表名]
     * @Author:     [fxx]
     * @CreateDate: [2017-5-16]
     * @Params:     [ParamType ParamValue,ParamType ParamValue...]
     * @See         [Related classes and methods]
     * @Since       [Product template version]  
     * @Return:     [List<TableInfoVo>]
     * @Throws:     [Exception]
     *
     */
	public List<TableInfoVo> getUserTablesName(DataSourceVo vo)
			throws NiceException, ServiceException {
		List<TableInfoVo> userTableList = new ArrayList<TableInfoVo>();
        try {
            dao.initDao(vo);
            //抽取授权的表和视图
            userTableList.addAll( dao.getUserTablesForSqlserverext());
        } catch (DaoException e) {
            throw new ServiceException("获取表元数据错误", e);
        }
        return userTableList;
	}

}
